//
//  NSDate+Extension.swift
//  AIPhotoProcess
//
//  Created by aw031 on 17/10/2025.
//

import Foundation


/// 日期相关扩展，提供常用的日期格式化、比较、区间判断等功能
extension Date {
    /// 将日期格式化为"YYYY/MM/dd"字符串
    func toYYYYMMDDString() -> String {
        let formatter = Date.defaultFormatter()
        formatter.dateFormat = "YYYY/MM/dd"
        return formatter.string(from: self)
    }
    
    /// 将日期格式化为"hh:mm a"（12小时制，AM/PM）
    func toHHMMAString() -> String {
        let formatter = Date.defaultFormatter()
        formatter.dateFormat = "hh:mm a"
        return formatter.string(from: self)
    }
    
    /// 将日期格式化为"hh:mm"（12小时制）
    func toHHMMString() -> String {
        let formatter = Date.defaultFormatter()
        formatter.dateFormat = "hh:mm"
        return formatter.string(from: self)
    }
   
    /// 将日期格式化为"hh:mm a"（12小时制，AM/PM）
    func toYYYY_MM_dd_HH_mm_ss_String() -> String {
        let formatter = Date.defaultFormatter()
        formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
        return formatter.string(from: self)
    }
   
    /// 将日期格式化为"hh:mm a"（12小时制，AM/PM）
    func toMM_dd_HH_mm_ss_String() -> String {
        let formatter = Date.defaultFormatter()
        formatter.dateFormat = "MM-dd HH:mm"
        return formatter.string(from: self)
    }
    
    
    /// 检查是否是今天创建的
    func isCreatedToday() -> Bool {
        let calendar = Calendar.current
        return calendar.isDateInToday(self)
    }
    
    /// 检查是否是最近7天创建的
    func isCreatedInLastWeek() -> Bool {
        let calendar = Calendar.current
        let now = Date()
        let sevenDaysAgo = calendar.date(byAdding: .day, value: -7, to: now) ?? now
        return self >= sevenDaysAgo
    }
    
    
    //判断俩个时间是否超过5天
     func isDateMoreThanFiveDaysApart(date: Date) -> Bool {
       return Date.isDateMoreThanFiveDaysApart(date1:date  ,date2:self)
    }
    //判断 date2 是否在 date1 的5天之后
   static func isDateMoreThanFiveDaysApart(date1: Date, date2: Date) -> Bool {
        let timeInterval = date1.timeIntervalSince(date2)
        let fiveDaysInSeconds: TimeInterval = 5 * 24 * 60 * 60
        return timeInterval > fiveDaysInSeconds
    }
    

    /// 获取当前日期是星期几（1-7，周日为7）
    func week() -> String {
        let calendar = Calendar.current
        let weekday = calendar.component(.weekday, from: self)
        let weekdays = ["7", "1", "2", "3", "4", "5", "6"]
        return weekdays[weekday - 1]
    }
    /// 获取当前日期是几号
    func day() -> String {
        let calendar = Calendar.current
        let day = calendar.component(.day, from: self)
        return "\(day)"
    }
    
    /// 比较当前日期是否晚于（或等于）目标日期的小时和分钟
    func isAfterHourMinute(of date: Date, inclusive: Bool = false) -> Bool {
        let calendar = Calendar.current
        let total1 = calendar.component(.hour, from: self) * 60 + calendar.component(.minute, from: self)
        let total2 = calendar.component(.hour, from: date) * 60 + calendar.component(.minute, from: date)
        return inclusive ? (total1 >= total2) : (total1 > total2)
    }
   
    /// 判断两个日期是否为同一天
    func isSameDay(as date: Date) -> Bool {
        let calendar = Calendar.current
        return calendar.isDate(self, inSameDayAs: date)
    }
   
    /// 获取几天后的日期
    func dateAfter(days: Int) -> Date? {
        Calendar.current.date(byAdding: .day, value: days, to: self)
    }
   
    /// 判断self日期是否在startDate之后（可选是否包含当天）
    func isAfter(startDate: Date, inclusive: Bool = false) -> Bool {
        let calendar = Calendar.current
        let selfDay = calendar.startOfDay(for: self)
        let startDay = calendar.startOfDay(for: startDate)
        return inclusive ? (selfDay >= startDay) : (selfDay > startDay)
    }
    /// 判断self日期是否在endDate之前（可选是否包含当天）
    func isBefore(endDate: Date, inclusive: Bool = false) -> Bool {
        let calendar = Calendar.current
        let selfDay = calendar.startOfDay(for: self)
        let endDay = calendar.startOfDay(for: endDate)
        return inclusive ? (selfDay <= endDay) : (selfDay < endDay)
    }
   
    /// 获取当前时间戳
    static func currentTimestamp() -> TimeInterval {
        Date().timeIntervalSince1970
    }
    /// 默认日期格式化器（en_US，系统时区）
    static func defaultFormatter() -> DateFormatter {
        let formatter = DateFormatter()
        formatter.locale = Locale(identifier: "en_US")
        formatter.timeZone = TimeZone.current
        return formatter
    }
    
    
    
    
    static func getFormattedCurrentTime() -> String {
        let formatter = DateFormatter()
        formatter.dateStyle = .medium
        formatter.timeStyle = .short
        formatter.dateFormat = "YYYY-MM-dd HH:mm:ss SSS"
        formatter.timeZone = TimeZone(identifier: "Asia/Shanghai")
        let datenow = Date() // 现在时间
        let timeInterval = Int64(datenow.timeIntervalSince1970 * 1000) // 注意运算顺序
        return "\(timeInterval)"
    }
    
    
    func getCurrentDate() -> String {
        let dateFormatter = DateFormatter()
        dateFormatter.locale = Locale(identifier: "en_US_POSIX")
        dateFormatter.dateFormat = "YYYYMMdd"
        dateFormatter.timeZone = TimeZone(secondsFromGMT: 0)
        return dateFormatter.string(from: Date())
    }
    
    static func getCurrentDate() -> TimeInterval {
      return  Date().timeIntervalSince1970
    }
    
}
